package cn.com.listenerInterface;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.broad.Board_seat;

import com.server.Board_Table;


/**
 * 桌子线程启动
 * 事件帧听
 * @author 
 */
public class StateListerImp implements StateListener 
{
	//
	private static Logger logger = LoggerFactory.getLogger(StateListerImp.class);
	
	public synchronized void demoEvent(Board_Table table) 
	{
		try
		{
			int tablestate = table.getBoard_state();
			if(tablestate == 1)
			{
				//table.balckOut();
				if(table.tableInit()>0)
				{
					table.startup();
				}
			}
			else if(tablestate == 5)
			{
				try
				{
					//Thread.currentThread().sleep(3000);
					Thread.sleep( 3000 );
				} catch (InterruptedException e) 
				{
					logger.error("  demoEvent  线程休眠出错");
				}
				//stable.getMessageManager().castGameOver(table);  //  广播游戏结束
				if(table.tableInit()>0)
				{
					table.startup();
				}
			}
		}catch (Exception e)
		{
		      e.printStackTrace();
			  logger.error(" event demoEvent 出错：{}", table.getBoard_state());
//			  table.getMessageManager().castGameOver(table );  //  广播游戏结束
			  List<Byte> list = new ArrayList<Byte>();
			  for(int i=0;i<table.getPoke_seat().size();i++)
			  {
				  if(((Board_seat)(table.getPoke_seat().get(i))).getUserthread() == null||
						  ((Board_seat)(table.getPoke_seat().get(i))).getUserthread().getUser() == null)
				  {
					  list.add((byte)((Board_seat)(table.getPoke_seat().get(i))).getId());
				  }
			  }
			  table.getMessageManager().clearSeat(table, list);
			  table.setIsplaying(false);
			  if(table.tableInit()>0)
			  {
				  	logger.error(" event demoEvent 出错之后游戏启动,桌子状态:{}", table.getBoard_state());
					table.startup();
			  }
		}
	}

}
